package com.example.edu_service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.R;
import com.example.edu_service.entity.EduCourse;
import com.example.edu_service.entity.EduTeacher;
import com.example.edu_service.entity.vo.CourseInfo;
import com.example.edu_service.entity.vo.CoursePublish;
import com.example.edu_service.entity.vo.CourseQuery;
import com.example.edu_service.entity.vo.TeacherQuery;
import com.example.edu_service.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/edu_service/edu_course")

public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;


    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfo courseInfo){

       String id= eduCourseService.addCourse(courseInfo);

        return R.ok().data("courseId",id);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
      CourseInfo courseInfo= eduCourseService.getCourseInfo(courseId);

      return R.ok().data("courseInfo",courseInfo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfo courseInfo){

        eduCourseService.updateCourseInfo(courseInfo);

        return R.ok();
    }

    @GetMapping("getCoursePublishInfo/{id}")
    public R getCoursePublishInfo(@PathVariable String id){
        CoursePublish coursePublish= eduCourseService.getCoursePublishInfo(id);
        return R.ok().data("coursePublishInfo",coursePublish);
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");

        eduCourseService.updateById(course);
        return R.ok();
    }



    @PostMapping("getCourseList/{current}/{limit}")
    public R getCourseList(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) CourseQuery courseQuery
                           ){
        Page<EduCourse> page = new Page<>(current, limit);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();

        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }

        wrapper.orderByDesc("gmt_create");

        eduCourseService.page(page,wrapper);

        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();

        return R.ok().data("total",total).data("records",records);

    }

    @DeleteMapping("deleteCourseInfo/{courseId}")
    public R deleteCourseInfo(@PathVariable String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

}

